package com.sinoglobal.sinostore.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.sinoglobal.sinostore.R;
import com.sinoglobal.sinostore.bean.SecondKillListVo.SecondKillVo;
import com.sinoglobal.sinostore.system.Constants;
import com.sinoglobal.sinostore.utils.BitmapHelp;
import com.sinoglobal.sinostore.utils.TextUtil;
/**
 * 
* @title: SecondKillGoodsAdapter.java 
* @package com.chiliring.sinostore.adapter 
* @description: TODO(秒杀商品订单) 
* @author LK
* @date 2015-1-14 上午10:09:47
 */
public class SecondKillGoodsAdapter extends BaseAdapter {
	private Context context;
	private List<SecondKillVo> data;
	private ImageLoader imageLoader;
	private DisplayImageOptions options;
	
	public SecondKillGoodsAdapter(Context context){
		this.context = context;
		data = new ArrayList<SecondKillVo>();
		imageLoader = ImageLoader.getInstance();
		options = new DisplayImageOptions.Builder()
					.showImageOnLoading(R.drawable.shop_defaultimgcollect_list_img)
					.showImageForEmptyUri(R.drawable.shop_defaultimgcollect_list_img)
					.showImageOnFail(R.drawable.shop_defaultimgcollect_list_img)
					.cacheInMemory(true)
					.considerExifParams(true)
					.build();
	}

	/**
	 * 
	* @author LK
	* @methods AddData 
	* @description 描述信息 添加数据
	* @date 2015-1-14 上午10:12:18
	* @param data 参数说明
	 */
	public void AddData(List<SecondKillVo> data){
		if(null != data){
			this.data.addAll(data);
		}
		notifyDataSetChanged();
	}
	/**
	 * 
	* @author LK
	* @methods clearData 
	* @description 描述信息  清理数据
	* @date 2015-1-19 下午2:55:45 参数说明
	 */
	public void clearData(){
		if(null != data){
			data.clear();
		}
		notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(null == convertView){
			convertView = LayoutInflater.from(context).inflate(R.layout.shop_item_secondkill_goods, null);
		}
		ImageView goodsPic = ViewHolder.getViewById(convertView, R.id.iv_goods_pic);
		ImageView ivSoldOut =ViewHolder.getViewById(convertView, R.id.iv_sold_out);
		TextView goodsName = ViewHolder.getViewById(convertView, R.id.tv_secondkill_goodsname);
		TextView secondkillPrice = ViewHolder.getViewById(convertView, R.id.tv_secondkill_price);
		TextView goodsPrice = ViewHolder.getViewById(convertView, R.id.tv_goods_price);
		goodsPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
		SecondKillVo entity = (SecondKillVo)getItem(position);
		imageLoader.displayImage(Constants.PIC_HOST+entity.img_url, goodsPic,options);
		goodsName.setText(entity.name);
		secondkillPrice.setText(String.format("秒杀价%s元", entity.ms_price));
		if(TextUtil.isZeroOrNull(entity.price) && !TextUtil.isZeroOrNull(entity.cash)){//积分单价为零，现金不为零，为现金支付
			goodsPrice.setText(String.format("原价%s元", entity.cash));
		}else if(TextUtil.isZeroOrNull(entity.cash) && !TextUtil.isZeroOrNull(entity.price)){//现金单价为零，为积分支付
			goodsPrice.setText(String.format("原价%s%s", entity.price,context.getString(R.string.score_unit)));
		}else{//两个都不为零则为现金加积分支付
			goodsPrice.setText(String.format("原价%s元+%s%s", entity.cash,entity.price,context.getString(R.string.score_unit)));
		}
		ivSoldOut.setVisibility(View.INVISIBLE);
		if(Constants.SOLDOUT.equals(entity.status)){
			ivSoldOut.setVisibility(View.VISIBLE);
		}
		return convertView;
	}

}
